<!DOCTYPE HTML>
<html>
    <head>
        <title>API请求</title>
        <meta charset="utf-8">
    </head>
    <body>
        <pre id="data"></pre>
    </body>
</html>
<script src = "js/jquery.min.js"></script>
<script>
var api = function(config) {
    var self = this;

    self.info = new Object();

    self.getUrlParam = function(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]);
        return null;
    }

    self.utc2localTime = function(time) {
        var time_string_utc_epoch = Date.parse(time);
        var unixTimestamp = new Date(time_string_utc_epoch);
        var year = unixTimestamp.getFullYear();
        var month = unixTimestamp.getMonth() + 1;
        var date = unixTimestamp.getDate();
        var hour = unixTimestamp.getHours();
        var minute = unixTimestamp.getMinutes();
        var second = unixTimestamp.getSeconds();
        hour = (hour<10)?'0'+hour:hour;
        minute = (minute<10)?'0'+minute:minute;
        second = (second<10)?'0'+second:second;
        return year+'/'+month+'/'+date+' '+hour+':'+minute+':'+second;
    }

    self.options = {
        id: null,
        menu: null,
        label: null,
        q: null,
        page: 1
    }

    self.set = function() {
        if (self.getUrlParam('id') != undefined) {
            self.options.id = parseInt(self.getUrlParam('id'));
        }
        if (self.getUrlParam('label') != undefined) {
            self.options.label = self.getUrlParam('label');
        }
        if (self.getUrlParam('menu') != undefined) {
            self.options.menu = self.getUrlParam('menu');
        }
        if (self.getUrlParam('q') != undefined) {
            self.options.q = self.getUrlParam('q');
        }
        if (self.getUrlParam('page') != undefined) {
            self.options.page = parseInt(self.getUrlParam('page'));
        }
    }

    self.set();

    self.HandleInfo = function() {
        
        if(self.options.id != null) {
            $.ajax({
                type: 'get',
                async: false,
                headers: {
                    Accept: 'application/vnd.github.squirrel-girl-preview, application/vnd.github.html+json, application/x-www-form-urlencoded',
                },
                url: 'https://api.github.com/repos/' + config.name + '/' + config.repo + '/issues/' + self.options.id,
                success: function(data) {
                    self.info.title = data.title;
                    self.info.time = self.utc2localTime(data.created_at);
                    data.body_html = data.body_html.replace(/</g,"&lt;");
                    data.body_html = data.body_html.replace(/>/g,"&gt;");
                    self.info.content = data.body_html;
                    self.info.labels = new Array();
                    for (var i in data.labels) {
                        var label = new Object()
                        label.name = data.labels[i].name;
                        self.info.labels.push(label);
                    }
                }
            });
            $.ajax({
                type: 'get',
                async: false,
                headers: {
                    Accept: 'application/vnd.github.squirrel-girl-preview+json'
                },
                url: 'https://api.github.com/repos/' + config.name + '/' + config.repo + '/issues/' + self.options.id + '/reactions?content=heart',
                success: function(data) {
                    self.info.like = data.length;
                }
            });
            self.info = JSON.stringify(self.info, null, "\t");
            document.getElementById('data').innerHTML = self.info;
        } else if(self.options.menu != null) {
            $.ajax({
                type: 'get',
                headers: {
                    Accept: 'application/json',
                },
                url: 'config.json',
                success: function(data) {
                    self.info = new Array();
                    for (var i in data.menu) {
                        var label = new Object();
                        label.name = i;
                        label.url = data.menu[i];
                        console.log(label)
                        self.info.push(label);
                    }
                    self.info = JSON.stringify(self.info, null, "\t");
                    document.getElementById('data').innerHTML = self.info;
                }
            });
        } else {
            var issue_url;
            var issue_perpage_url;
            self.info.page = self.options.page;
            if (self.options.label == undefined) {
                if (self.options.q == undefined) {
                    issue_url = 'https://api.github.com/repos/' + config.name + '/' + config.repo;
                    issue_perpage_url = 'https://api.github.com/repos/' + config.name + '/' + config.repo + '/issues?creator=' + config.name + '&';
                } else {
                    var search = encodeURI(self.options.q);
                    issue_url = 'https://api.github.com/search/issues?q=' + search + ' author:' + config.name + '+in:title,body';
                    issue_perpage_url = 'https://api.github.com/search/issues?q=' + search + ' author:' + config.name + '+in:title,body&';
                }
            } else {
                issue_url = 'https://api.github.com/repos/' + config.name + '/' + config.repo + '/issues?labels=' + self.options.label;
                issue_perpage_url = 'https://api.github.com/repos/' + config.name + '/' + config.repo + '/issues?creator=' + config.name + '&labels=' + self.options.label + '&';
                
            }
            $.ajax({
                type: 'get',
                url: issue_url,
                async: false,
                success: function(data) {
                    var itemNum = 0;
                    if (self.options.label != null && self.options.label != undefined) {
                        if (self.options.q != null && self.options.q != undefined) {
                            itemNum = data.total_count;
                        } else {
                            itemNum = data.length;
                        }
                    } else if (self.options.id != null && self.options.id != undefined) {
                        itemNum = data.length;
                        document.getElementById('comments-num').innerHTML = itemNum;
                    } else {
                        itemNum = data.open_issues_count;
                    }
                    if (itemNum > 10) {
                        self.info.page_num = Math.ceil(itemNum / 10);
                    }
                }
            });
            $.ajax({
                type: 'get',
                async: false,
                url: issue_perpage_url + 'page=' + self.options.page + '&per_page=10',
                success: function(data) {
                    self.info.article = new Array();
                    if (self.options.q != undefined && self.options.q != null) {
                        data = data.items;
                    }
                    for(var i in data) {
                        var item = new Object();
                        item.id = data[i].number;
                        item.time = self.utc2localTime(data[i].created_at);
                        item.title = data[i].title;
                        item.author = data[i].user.login;
                        data[i].body = data[i].body.replace(/</g,"&lt;");
                        data[i].body = data[i].body.replace(/>/g,"&gt;");
                        item.content = data[i].body;
                        item.labels = new Array();
                        for(var j in data[i].labels) {
                            var label = new Object();
                            label.name = data[i].labels[j].name;
                            item.labels.push(label);
                        }
                        self.info.article.push(item);
                    }
                }
            });
            self.info = JSON.stringify(self.info, null, "\t");
            document.getElementById('data').innerHTML = self.info;
        }
    }

    self.HandleInfo();
}

$.ajax({
    type: 'get',
    headers: {
        Accept: 'application/json',
    },
    url: 'config.json',
    success: function(data) {
        new api(data);
    }
});
</script>